package example.demo.forkjoin;

import example.demo.bean.Student;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ReturnForkJoinTest {
    public static void main(String[] args) {
        List<Student> studentList = IntStream.rangeClosed(0, 100)
                .mapToObj(i -> "name" + i)
                .map(s -> new Student(s, Integer.parseInt(s.split("name")[1])))
                .collect(Collectors.toList());
        ForkJoinPool pool = new ForkJoinPool();
        ReturnTask returnTask = new ReturnTask(0, studentList.size() - 1, studentList);

        ForkJoinTask<List<Student>> forkJoinTask = pool.submit(returnTask);
        List<Student> students = null;
        try {
            students = forkJoinTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        pool.shutdown();
        if (null != students) {
            students.forEach(System.out::println);
            System.out.println("studentList==students = " + (studentList == students));
        }
    }
}
